import { put, take } from 'redux-saga/effects';
import types from './types';
import authTypes from '../auth/types';
import Storage from '../../utils/storage';

let Sagas = {};

/**
 * 登录调用，不会触发页面渲染。
 * 登录成功后调用获取更新用户信息
 */
Sagas['UserSaga'] = function* () {
  while (true) {
    // 拦截到个人信息的请求
    const { payload } = yield take(types.GetUserInfo);
    if (payload && payload.token) {
      const token = payload.token;
      const userinfo = yield Storage.load('userinfo', { token });
      if (userinfo) {
        yield put({ type: types.RenderData, payload: userinfo });
      } else {
        yield put({ type: authTypes.Logout });
      }
    }
  }
};

export default Sagas;
